package com.ashin;

import com.ashin.generator.bean.User;
import com.ashin.generator.dao.FilmMapper;
import com.ashin.generator.dao.UserMapper;
import com.ashin.util.DBUtil;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.List;

/**
 * Hello world!
 */
public class MybatisAnnotationGenerator {

    public static void main(String[] args) throws IOException {
        DataSource dataSource = DBUtil.getInstance().getDataSource();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(FilmMapper.class);
        configuration.addMapper(UserMapper.class);
        SqlSessionFactory sqlSessionFactory1 = new SqlSessionFactoryBuilder().build(configuration);

        SqlSession sqlSession = sqlSessionFactory1.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.all();
        for (User user : users) {
            System.out.println(user);
        }

        System.out.println("film 演示");
        FilmMapper mapper = sqlSession.getMapper(FilmMapper.class);


        mapper.select(SelectDSLCompleter.allRows()).forEach(System.out::println);
        System.out.println("=========================");
        mapper.all().forEach(System.out::println);



        sqlSession.commit();
        sqlSession.close();


    }
}
